package com.bao.work.jin.service.impl;

import com.bao.work.jin.entity.Goods;
import com.bao.work.jin.service.GoodsService;

import java.math.BigDecimal;
import java.util.TreeMap;

/**
 * @author baoye
 * @date 2024-08-02/17:08
 */
public class GoodsServiceImpl implements GoodsService {
    static {
        GOODS_TREE.put(new Goods("1111","蜘蛛王皮鞋",new BigDecimal("238.0"),"黑色",42),100000);
        GOODS_TREE.put(new Goods("3333","Thinkpad x240",new BigDecimal("5939.0"),"黑色",12),50);
        GOODS_TREE.put(new Goods("2222","iPhone 7Plus",new BigDecimal("5686.0"),"白色",5),5000);
    }
    @Override
    public void addGoods() {
        System.out.println("请输入商品编号");
        String goodsId=INPUT.next();
        INPUT.nextLine();
        System.out.println("请输入商品名称");
        String goodsName=INPUT.nextLine();
        System.out.println("请输入商品价格");
        BigDecimal price=new BigDecimal(INPUT.next());
        System.out.println("请输入商品颜色");
        String color=INPUT.next();
        System.out.println("请输入商品尺寸");
        int size=INPUT.nextInt();
        System.out.println("请输入商品库存");
        int num = INPUT.nextInt();

        if(GOODS_TREE.put(new Goods(goodsId,goodsName,price,color,size),num)==null){
            System.out.println("添加成功");
        }

    }

    @Override
    public void selectGoods() {
        GOODS_TREE.keySet().forEach(s-> System.out.println(s+"库存:"+GOODS_TREE.get(s)));
    }

    @Override
    public void selectGoodsById() {
        System.out.println("请输入需要查找的商品编号");
        String id=INPUT.next();
        for (Goods goods : GOODS_TREE.keySet()) {
            if(goods.getGoodsId().equals(id)){
                System.out.println(goods);
                return;
            }
        }
        System.out.println("您查找的商品不存在");
    }

    @Override
    public void insertGoodsToCart() {
        System.out.println("请输入您要添加的商品编号");
        Goods addGoods=new Goods();
        String id=INPUT.next();
        System.out.println("请输入购买数量");
        Integer num=0;
        for (Goods goods : GOODS_TREE.keySet()) {
            if(goods.getGoodsId().equals(id)){
                num=INPUT.nextInt();
                if(num>GOODS_TREE.get(goods)){
                    System.out.println("超过库存");
                    return;
                }
                addGoods= goods;
                GOODS_TREE.put(goods,GOODS_TREE.get(goods)-num);
            }
        }

        if(addGoods.getGoodsId()==null){
            System.out.println("商品编号不存在");
            return;
        }
        if (CART_TREE.get(addGoods)==null){

            CART_TREE.put(addGoods,num);
        }else {
            CART_TREE.put(addGoods,CART_TREE.get(addGoods)+num);
        }
        System.out.println("添加成功");

    }

    @Override
    public void selectCart() {
        CART_TREE.keySet().forEach(s-> System.out.println(s+"库存"+CART_TREE.get(s)));
    }

}
